package com.sbhotel.chotellauncher.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.TextView;

import com.sbhotel.chotellauncher.R;
import com.sbhotel.chotellauncher.widget.AdapterMetroView;

import java.util.Timer;
import java.util.TimerTask;

public class WarningTVActivity extends BaseActivity {

    TextView tvBtn;
    AdapterMetroView tv_btn_layout;
    Timer timer;
    private static final String TV_PKG = "com.changhong.tvap.atv.activity";
    private static final String TV_CLS = "com.changhong.tvap.atv.activity.AtvMainActivity";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        timer = new Timer(true);
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                time--;
                handler.sendEmptyMessage((time == 0 ? 1 : 0));
            }
        }, 1000, 1000);
    }

    //设定倒计时时长 n 单位 s
    private int time = 5;
    //初始化 Handler
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    tvBtn.setText("知道了 " + time + "s");
                    break;
                case 1:
                    try {
                        Intent intent = new Intent();
                        intent.setClassName(TV_PKG, TV_CLS);
                        startActivity(intent);
                        finish();
                    } catch (Exception e) {
                        e.printStackTrace();
                        finish();
                    }
            }
            super.handleMessage(msg);
        }
    };

    @Override
    public void initView() {
        tvBtn = findViewById(R.id.button_tv);
        tvBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                timer.cancel();
                try {
                    Intent intent = new Intent();
                    intent.setClassName(TV_PKG, TV_CLS);
                    startActivity(intent);
                    finish();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

        tv_btn_layout = findViewById(R.id.tv_btn_layout);
        tv_btn_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                timer.cancel();
                try {
                    Intent intent = new Intent();
                    intent.setClassName(TV_PKG, TV_CLS);
                    startActivity(intent);
                    finish();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    public void initData() {
        initTitleBar("温馨提示", "");
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

    @Override
    public int initLayout() {
        return R.layout.activity_tv_warning;
    }

    @Override
    protected void onDestroy() {
        timer.cancel();
        super.onDestroy();

    }
}
